package cn.tedu.kcircle.attachment.infra.repository;

import cn.tedu.kcircle.attachment.dao.entity.AttachDO;
import cn.tedu.kcircle.attachment.dao.entity.AttachLogDO;
import cn.tedu.kcircle.attachment.dao.mapper.AttachLogMapper;
import cn.tedu.kcircle.attachment.dao.mapper.AttachMapper;
import cn.tedu.kcircle.attachment.infra.converter.AttachmentInfraConverter;
import cn.tedu.kcircle.attachment.infra.minio.MinioTemplate;
import cn.tedu.kcircle.attachment.repository.model.AttachAddModel;
import cn.tedu.kcircle.attachment.repository.model.AttachLogAddModel;
import cn.tedu.kcircle.attachment.repository.persist.AttachmentRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Repository
@AllArgsConstructor
@Slf4j
public class AttachRepositoryImpl implements AttachmentRepository {
    private MinioTemplate minioTemplate;
    private AttachMapper attachMapper;
    private AttachLogMapper attachLogMapper;
    private AttachmentInfraConverter converter;

    @Override
    public List<String> uploadBatch(MultipartFile[] multipartFile) {
        return minioTemplate.upload(multipartFile);
    }

    @Override
    public Long attachLogAdd(AttachLogAddModel attachLogAddModel) {
        AttachLogDO attachLogDO = converter.model2DO(attachLogAddModel);
        attachLogMapper.insert(attachLogDO);
        return attachLogDO.getId();
    }

    @Override
    public Long insertAttach(AttachAddModel attachAddModel) {
        AttachDO attachDO = converter.model2DO(attachAddModel);
        attachMapper.insert(attachDO);
        return attachDO.getId();
    }

    @Override
    public List<Long> insertBatchAttach(List<AttachAddModel> attachAddModels) {
        List<AttachDO> attachDOS = converter.model2AttachDOS(attachAddModels);
        attachMapper.insertBatch(attachDOS);
        return attachDOS.stream().map(AttachDO::getId).toList();
    }

    @Override
    public List<Long> insertBatchAttachLog(List<AttachLogAddModel> attachLogAddModels) {
        List<AttachLogDO> attachLogDOS = converter.model2AttachLogDOS(attachLogAddModels);
        attachLogMapper.insertBatch(attachLogDOS);
        return attachLogDOS.stream().map(AttachLogDO::getId).toList();
    }


}
